package com.ruoyi.system.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ruoyi.system.config.WechatPayConfig;
import com.ruoyi.system.service.IWechatPay;
import okhttp3.HttpUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.Signature;
import java.util.Base64;
import java.util.HashMap;
import java.security.Signature;
import java.util.Base64;
import java.util.UUID;

/**
 *
 * @ClassName WechatPayServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author luqian
 * @Date 2024/6/27 4:08 PM
 * @Copyright 八维
 * @Version V1.0.0
 */
@Service
public class WechatPayServiceImpl implements IWechatPay {
    @Autowired
    private WechatPayConfig wechatPayConfig;


   /* @Autowired
    private WxPayService wxService;*/
//    @Override
//    public String wechatPay(String orderNum, BigDecimal price, String title) {
//        //TODO 健壮性校验
//        //1。请求地址
//        //2。请求参数
//        //3。请求方式
//        //4。响应结果
//
//        //请求URL：https://api.mch.weixin.qq.com/v3/pay/transactions/native
//        //请求方式：POST
//        /*HashMap<String, Object> params = new HashMap<>();
//        params.put("appid",wechatPayConfig.getAppid());
//        params.put("mchid",wechatPayConfig.getMchid());
//        params.put("description",title);
//        params.put("out_trade_no",orderNum);
//        params.put("notify_url",wechatPayConfig.getNotifyUrl());
//        params.put("amount",new HashMap<String,Object>(){{
//            put("total",price.multiply(new BigDecimal(100)).intValue());
//            put("currency","CNY");
//        }});
//        String paramJson = JSONUtil.toJsonStr(params);
//        HttpRequest httpRequest = HttpUtil.createPost(wechatPayConfig.getNativePayUrl());
//        httpRequest.header("Content-Type","application/json");
//        httpRequest.header("Accept","application/json");
//        httpRequest.header("Authorization","WECHATPAY2-SHA256-RSA2048 "+wechatPayConfig.getApiKey());
//        httpRequest.body(paramJson);
//        HttpResponse execute = httpRequest.execute();*/
////        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
////        wxPayUnifiedOrderRequest.setAppid(wechatPayConfig.getAppid());
////        wxPayUnifiedOrderRequest.setMchId(wechatPayConfig.getMchid());
////        wxPayUnifiedOrderRequest.setNonceStr(UUID.randomUUID().toString());
////        wxPayUnifiedOrderRequest.setSign();
////        wxPayUnifiedOrderRequest.setBody(title);
////        wxPayUnifiedOrderRequest.setOutTradeNo(orderNum);
////        wxPayUnifiedOrderRequest.setTotalFee(price.multiply(new BigDecimal(100)).intValue());
////        wxPayUnifiedOrderRequest.setSpbillCreateIp("123.12.12.123");
//
//
//        return execute.body();
//    }


    @Bean
    public WxPayService getWxService() {
        WxPayServiceImpl wxPayServiceImpl = new WxPayServiceImpl();
        WxPayConfig config = new WxPayConfig();
        config.setAppId(wechatPayConfig.getAppid());
        config.setMchId(wechatPayConfig.getMchid());
        config.setNotifyUrl(wechatPayConfig.getNativePayUrl());
        config.setSignType("MD5");
        config.setTradeType("NATIVE");
        config.setMchKey(wechatPayConfig.getApiKey());
        wxPayServiceImpl.setConfig(config);
        return wxPayServiceImpl;
    }

}
